package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.other.BaseController;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.Employee;
import com.example.domain.Setmeal;
import com.example.result.JsonResult;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.service.SetmealService;
import com.example.untils.RedisContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类表
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class  CategoryController extends BaseController {   //分类表
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //新增菜品分类
    @PostMapping("/save")
    public JsonResult save(@RequestBody Category category){
        System.out.println(category.toString());
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,category.getName());
        Category result = categoryService.getOne(queryWrapper);
        if(result!=null){
            return new JsonResult(0,"此分类已存在!");
        }
        boolean flag = categoryService.save(category);
        if(!flag){
            return new JsonResult(0,"新增失败,请重试!");
        }
        stringRedisTemplate.opsForList().rightPush(RedisContent.CATEGORY_KEY, JSONUtil.toJsonStr(category));
        return new JsonResult(1,"新增成功");
    }
    //分类分页查询
    @GetMapping("/page")
    public JsonResult page(Integer currentPage,Integer pageSize){
        if(currentPage==null||pageSize==null){
            return new JsonResult(0,"当前页或者当前最大页数错误");
        }
        IPage<Category> iPage=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(iPage,queryWrapper);
        return new JsonResult(1,"",iPage);
    }
    //删除分类
    @DeleteMapping("/deleteById/{id}")
    public JsonResult delete(@PathVariable Long id){
        System.out.println(".................."+id);
        LambdaQueryWrapper<Dish> dishWrapper=new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        List<Dish> list1 = dishService.list(dishWrapper);
        if(list1.size()!=0){
            return new JsonResult(0,"此分类与菜品有关联");
        }
        LambdaQueryWrapper<Setmeal> setmealWrapper=new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        List<Setmeal> list2 = setmealService.list(setmealWrapper);
        if(list2.size()!=0){
            return new JsonResult(0,"此分类与套餐有关联");
        }
        Boolean flag = categoryService.removeById(id);
        if(!flag){
            return new JsonResult(0,"删除失败,请重试!");
        }
        stringRedisTemplate.opsForList().remove(RedisContent.CATEGORY_KEY,0,-1);
        return new JsonResult(1,"删除成功");
    }
    //修改分类
    @PostMapping("/update")
    public JsonResult update(@RequestBody Category category){
        boolean flag = categoryService.updateById(category);
        if(!flag){
            return new JsonResult(0,"更新失败!,请重试");
        }
        stringRedisTemplate.opsForList().remove(RedisContent.CATEGORY_KEY,0,-1);
        return new JsonResult(1,"更新成功");
    }
    //根据ID获取分类信息
    @GetMapping("/gainByID/{id}")
    public JsonResult getById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        if(category==null){
            return new JsonResult(0,"分类信息不存在，请刷新!");
        }
        return new JsonResult(1,"",category);
    }
    //根据type获取分类列表
    @GetMapping("/gainCategoryList")
    public JsonResult gainCategoryList(Integer type){
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> list = categoryService.list(queryWrapper);
        return new JsonResult(1,"",list);
    }
}
